global name "B5_racemode"
cap log close
log using "${pfile}/${name}.smcl", replace smcl


// *** // Identifying the mode race for each individual in the data // *** //
use "B2_mergeadmin.dta", clear
keep cpf
sort cpf
by cpf: g co = _n
keep if co == 1
drop co
save B5_temp, replace

forvalues year = 2003/2017 { // 2002 does not have race var.
use "${prais}/`year'/2_`year's.dta", clear
keep cpf race causadesli tipoadm tpv natj
merge m:1 cpf using B5_temp, keep(3) nogen
replace race = 9 if race == 99
replace race = 9 if race == -1
save B5_temp_`year', replace
}

clear
forvalues year = 2003/2017 {
append using B5_temp_`year'
}

save B5_race, replace


// *** // Compute mode // *** //
use B5_race, clear

g racedismb = race if causadesli==11

foreach x in racedismb { 
egen `x'modemax = mode(`x'), by(cpf) maxmode
drop `x'
}

keep cpf racedismbmode* 
sort cpf
by cpf: g co = _n
keep if co == 1

save B5_racemode, replace


forvalues year = 2003/2017 {
erase B5_temp_`year'.dta
}
erase B5_temp.dta
*/




cap log close
